#!/usr/bin/env zsh
set -o pipefail

function usage()
{
    echo "usage: igsg <username>"
    exit
}

funciton username_error()
{
    echo "[ERR.] Illegal character in username string"
    exit
}

###############################################################################

[[ -z "${1}" ]] && usage 
[[ "${1}" =~ [^a-zA-Z0-9] ]] && username_error || username="${1}"

###############################################################################

function get_hl_ids()
{
    wget https://imginn.com/stories/${username}/ -qO- /dev/null \
        | grep -Eo 'id=\"[0-9]{1,}' \
        | awk -F "\"" '{print $2}' \
        | uniq \
        | tail -n +2
}

function get_hl_titles()
{
    wget https://imginn.com/stories/${username}/ -qO- /dev/null \
            |  grep -Eo 'alt="(.*?)"' \
            | awk -F "\"" '{print $2}' \
            | uniq \
            | tail -n +2
}

function get_story_id()
{
	wget https://imginn.com/stories/${username}/ -qO- /dev/null \
	    | grep -Eo 'id=\"[0-9]{1,}' \
	    | awk -F "\"" '{print $2}' \
	    | uniq \
	    | head -n 1
}

###############################################################################
###############################################################################
###############################################################################
###############################################################################

printf "[%s] Initiating\n" "${username}"

datestamp=$(date +"%Y-%m-%d")
IFS=$'\n'

hl_titles=($(get_hl_titles))

hl_ids=($(get_hl_ids))
[ "${#hl_titles[@]}" -lt 1 ] && printf "[%s] No Highlights Found\n" "${username}"

hl_dir="${HOME}"/.igsg/"${username}"/Highlights/"${datestamp}"
story_dir="${HOME}"/.igsg/"${username}"/Stories/"${datestamp}"

###############################################################################

printf "[%s] Creating Directories\n" "${username}"

mkdir -p "${story_dir}" "${hl_dir}" 2>/dev/null
[ -d "${story_dir}" ] && [ -d "${hl_dir}" ]  && : || mkdir_error

###############################################################################

printf "[%s] Getting Highlights\n" "${username}"

for i in {1.."${#hl_ids[@]}"}; do
    mkdir -p "${hl_dir}"/"${hl_titles[${i}]}" 2>/dev/null
    [ ! -d "${hl_dir}"/"${hl_titles[${i}]}" ] && echo "[ERR.] Couldn't make ${hl_titles[${i}]}" \
        && exit
    wget "https://imginn.com/api/high/?reelId=$hl_ids[$i]&lang=en" -qO- /dev/null \
        | json_pp \
        | grep -v thumb \
        | grep src \
        | awk -F "\"" '{print $4}' \
        | xargs -n 1 wget -P "${hl_dir}"/"${hl_titles[${i}]}" 2>/dev/null
done

###############################################################################

printf "[%s] Sorting Highlights\n" "${username}"

for j in {1.."${#hl_titles[@]}"}; do
    hl_jpg=($(find "${hl_dir}"/"${hl_titles[${j}]}" -iname "*.jpg*" \
                -maxdepth 1 2>/dev/null))
    hl_mp4=($(find "${hl_dir}"/"${hl_titles[${j}]}" -iname "*.mp4*" \
                -maxdepth 1 2>/dev/null))
    
    for k in {1..${#hl_jpg[@]}}; do
        [ "${#hl_jpg[@]}" -eq 0 ] && break
        
        file_name=$(LC_CTYPE=C tr -dc '[:alnum:]' < /dev/urandom | head -c 4)
        mv "${hl_jpg[${k}]}" "${hl_dir}"/"${hl_titles[${j}]}"/"${file_name}".jpg 2>/dev/null        
    done

    for l in {1..${#hl_mp4[@]}}; do   
            [ "${#hl_mp4[@]}" -eq 0 ] && break
            
            file_name=$(LC_CTYPE=C tr -dc '[:alnum:]' < /dev/urandom | head -c 4)
            mv "${hl_mp4[${l}]}" "${hl_dir}"/"${hl_titles[${j}]}"/"${file_name}".mp4 2>/dev/null           
    done    
done

###############################################################################

printf "[%s] Getting Stories\n" "${username}"

story_id=$(get_story_id)

wget "https://imginn.com/api/high/?id=$story_id&lang=en" -qO- /dev/null \
       | json_pp \
       | grep -v thumb \
       | grep src \
       | awk -F "\"" '{print $4}' \
       | xargs -n 1 wget -P "${story_dir}" 2>/dev/null

###############################################################################

printf "[%s] Sorting Stories\n" "${username}"

story_jpg=($(find "${story_dir}" -iname "*.jpg*" -maxdepth 1 2>/dev/null))
story_mp4=($(find "${story_dir}" -iname "*.mp4*" -maxdepth 1 2>/dev/null))

for m in {1..${#story_jpg[@]}}; do
        [ "${#story_jpg[@]}" -eq 0 ] && printf "[%s] No Stories Found\n" \
            "${username}" && break
        
        file_name=$(LC_CTYPE=C tr -dc '[:alnum:]' < /dev/urandom | head -c 4)
        mv "${story_jpg[${m}]}" "${story_dir}"/"${file_name}".jpg 2>/dev/null        
    done
    
for n in {1..${#story_mp4[@]}}; do
        [ "${#story_mp4[@]}" -eq 0 ] && break
        
        file_name=$(LC_CTYPE=C tr -dc '[:alnum:]' < /dev/urandom | head -c 4)
        mv "${story_mp4[${n}]}" "${story_dir}"/"${file_name}".mp4 2>/dev/null        
    done

###############################################################################

printf "[%s] Complete" "${username}"

